// 71. 简化路径
package Leetcode_Second_Brush;

import java.util.Deque;
import java.util.LinkedList;

public class Solution071 {
    public String simplifyPath(String path) {
        String[] split = path.split("/+");
        Deque<String> stack = new LinkedList<>();
        for (String s : split) {
            if ("..".equals(s) && !stack.isEmpty()) {
                stack.pop();
                continue;
            }
            if (".".equals(s) || "".equals(s) || "..".equals(s))
                continue;
            stack.push(s);
        }

        if (stack.isEmpty())
            return new String("/");

        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            sb.append("/").append(stack.removeLast());
        }

        return sb.toString();
    }
}
